﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void SaveCustomValueFunc(bool isTure,int second);

public class CountDownPanel : MonoBehaviour
{
    private static string prefabPath = "Prefabs/countDownPanel";
    private static CountDownPanel mInstance = null;
    public static CountDownPanel Instance
    {
        get
        {
            if (mInstance == null)
            {
                var go = Resources.Load(prefabPath) as GameObject;
                go = GameObject.Instantiate<GameObject>(go);
                go.name = "CountDownPanel";
                mInstance = go.AddComponent<CountDownPanel>();

                var trans = go.transform;
                trans.SetParent(NGUITools.TryGetUIRoot().transform);
                trans.localScale = Vector3.one;
                trans.localPosition = Vector3.one;
                trans.localEulerAngles = Vector3.zero;
            }
            return mInstance;
        }
    }

    private UIButton endBtn = null;
    private UILabel endBtnLab = null;

    private UIButton breakBtn = null;
    private UILabel breakBtnLab = null;

    private UIButton finishBtn = null;
    private UILabel finishBtnLab = null;
    private Vector3 originFinishPos = Vector3.zero;

    private UIButton shildBtn = null;

    private UIEventListener countDownEventListen = null;
    private Transform countDownBgSpr = null;
    private UISprite countDownFilled = null;
    private UILabel countDownLab = null;

    private UIButton signRadio = null;
    private TweenAlpha signTure = null;

    private UILabel upDesLab = null;
    private UILabel downDesLab = null;

    private bool isCanClickCountDownClock = false;
    private InvokeFunc clickHourFunc = null;
    private int countDownMinute = 0;
    private int countDownSecond = 0;

    private bool isShowDrawForCustomCountDownTime = false;
    private bool isShowAutoSaveCustomValueWhenDraw = false;
    private SaveCustomValueFunc saveCustomValueFunc = null;
    private bool isCountDownBgSprAutoRotate = false;


    //private bool 

    public void Awake()
    {
        endBtn = transform.Find("endBtn").GetComponent<UIButton>();
        endBtnLab = endBtn.transform.Find("Label").GetComponent<UILabel>();

        breakBtn = transform.Find("breakBtn").GetComponent<UIButton>();
        breakBtnLab = breakBtn.transform.Find("Label").GetComponent<UILabel>();

        finishBtn = transform.Find("finishBtn").GetComponent<UIButton>();
        finishBtnLab = finishBtn.transform.Find("Label").GetComponent<UILabel>();
        originFinishPos = finishBtn.transform.localPosition;

        shildBtn = transform.Find("shildBtn").GetComponent<UIButton>();

        countDownEventListen = transform.Find("countDownWidget").GetComponent<UIEventListener>();
        var countDownTrans = countDownEventListen.transform;
        countDownBgSpr = countDownTrans.Find("countDownBgSpr").transform;
        countDownFilled = countDownTrans.Find("countDownFilled").GetComponent<UISprite>();
        countDownLab = countDownTrans.Find("countDownLab").GetComponent<UILabel>();

        signRadio = countDownTrans.Find("signRadio").GetComponent<UIButton>();
        signTure = signRadio.transform.Find("signTure").GetComponent<TweenAlpha>();

        upDesLab = countDownTrans.Find("desWidget/upDesLab").GetComponent<UILabel>();
        downDesLab = countDownTrans.Find("desWidget/downDesLab").GetComponent<UILabel>();
    }

    void OnEnable()
    {
        countDownEventListen.onClick = OnClickHourClock;
        countDownEventListen.onDrag = OnDrag;
        signRadio.onClick.Add(new EventDelegate(OnClickRadio));
        //signTure.onFinished.Add(new EventDelegate(RadioPlayFinish));
        isCanClickCountDownClock = false;
        clickHourFunc = null;
        countDownMinute = 0;
        countDownSecond = 0;
        isShowDrawForCustomCountDownTime = false;
        isShowAutoSaveCustomValueWhenDraw = false;
        saveCustomValueFunc = null;
        isCountDownBgSprAutoRotate = false;
    }

    void OnDisable()
    {
        countDownEventListen.onClick = null;
        countDownEventListen.onDrag = null;
        signRadio.onClick.Clear();
        signTure.onFinished.Clear();
        isCanClickCountDownClock = false;
        clickHourFunc = null;
        countDownMinute = 0;
        countDownSecond = 0;
        isShowDrawForCustomCountDownTime = false;
        isShowAutoSaveCustomValueWhenDraw = false;
        saveCustomValueFunc = null;
        isCountDownBgSprAutoRotate = false;
    }

    void Update()
    {
        if (isCountDownBgSprAutoRotate) this.countDownBgSpr.Rotate(new Vector3(0, 0, 1) * -10 * Time.deltaTime);
    }

    void OnClickHourClock(GameObject go)
    {
        if (this.isCanClickCountDownClock)
        {
            if (this.clickHourFunc != null)
            {
                this.clickHourFunc();
            }
            SetClickCountDownClock();
        }
    }

    void OnDrag(GameObject go, Vector2 delte)
    {
        if (!this.isShowDrawForCustomCountDownTime) return;

        var addVal = delte.y;
        if (addVal != 0)
        {
            countDownSecond = countDownSecond + (int)addVal;// *10;
            if (countDownSecond >= 60 || countDownSecond < 0)
            {
                int addMin = (int)System.Math.Floor(countDownSecond / 60f);
                this.countDownMinute += addMin;
                if (this.countDownMinute > 59) this.countDownMinute = 59;
                else if (this.countDownMinute < 0) this.countDownMinute = 0;

                countDownSecond %= 60;
                if (countDownSecond < 0) countDownSecond = 60 + countDownSecond;
            }

            if (countDownSecond >= 60)
            {
                Debug.Log(countDownSecond);
            }

            SetCountDownTime(this.countDownMinute, this.countDownSecond);
            if (this.isShowAutoSaveCustomValueWhenDraw)
            {
                SetAutoSaveCustomValue(true);
                if (this.saveCustomValueFunc != null) this.saveCustomValueFunc(false, this.countDownMinute * 60 + this.countDownSecond);
            }
        }
    }

    private void OnClickRadio()
    {
        signTure.onFinished.Add(new EventDelegate(RadioPlayFinish));
        signTure.ResetToBeginning();
        signTure.PlayForward();
        signTure.enabled = true;
        if (this.saveCustomValueFunc != null)
        {
            this.saveCustomValueFunc(true, this.countDownMinute * 60 + this.countDownSecond);
        }
    }

    private void RadioPlayFinish()
    {
        signTure.onFinished.Clear();
        SetAutoSaveCustomValue(false);
    }

    /// <summary>
    /// 设置倒计时时间
    /// </summary>
    /// <param name="countDownMinute"></param>
    public void SetCountDownTime(int countDownMinute = 25,int countDownSecond = 0, float precent = 1f)
    {
        this.countDownMinute = countDownMinute;
        this.countDownSecond = countDownSecond;
        countDownLab.text = string.Format("{0:D2}:{1:D2}",countDownMinute , countDownSecond);
        countDownFilled.fillAmount = precent;
    }

    /// <summary>
    /// 设置点击时钟事件
    /// </summary>
    /// <param name="isCanClickCountDownClock"></param>
    /// <param name="clickDes"></param>
    /// <param name="clickHourFunc"></param>
    public void SetClickCountDownClock(bool isCanClickCountDownClock = false, InvokeFunc clickHourFunc = null, string clickDes = "点我开始倒计时") 
    {
        downDesLab.text = clickDes;
        this.isCanClickCountDownClock = isCanClickCountDownClock;
        this.clickHourFunc = clickHourFunc;

        downDesLab.gameObject.SetActive(isCanClickCountDownClock);
    }

    /// <summary>
    /// 设置上下拖动时间触发的事件
    /// </summary>
    /// <param name="isShowDrawForCustomCountDownTime">是否能够上下滑动时钟调整倒计时时间</param>
    /// <param name="isShowAutoSaveCustomValueWhenDraw">当滑动时钟时，是否需要显示保存自定义值的按钮</param>
    /// <param name="saveCustomValueFunc">自动保存事件回调</param>
    public void SetDragClock(bool isShowDrawForCustomCountDownTime = false, bool isShowAutoSaveCustomValueWhenDraw = false, SaveCustomValueFunc saveCustomValueFunc = null)
    {
        this.isShowDrawForCustomCountDownTime = isShowDrawForCustomCountDownTime;
        this.isShowAutoSaveCustomValueWhenDraw = isShowAutoSaveCustomValueWhenDraw;
        this.saveCustomValueFunc = saveCustomValueFunc;
        this.upDesLab.gameObject.SetActive(isShowDrawForCustomCountDownTime);
    }

    /// <summary>
    /// 设置背景圆圈是否自转
    /// </summary>
    /// <param name="isRotate">true：自转</param>
    /// <param name="isBackZero">角度回到原点</param>
    public void SetRotateAngle(bool isRotate = false, bool isBackZero = true) 
    {
        this.isCountDownBgSprAutoRotate = isRotate;
        if (isBackZero)
        {
            this.countDownBgSpr.localEulerAngles = Vector3.zero;
        }
    }

    /// <summary>
    /// 设置自动保存默认值按钮是否显示
    /// </summary>
    /// <param name="isShow">是否显示</param>
    public void SetAutoSaveCustomValue(bool isShow = false)
    {
        signRadio.gameObject.SetActive(isShow);
        signTure.enabled = false; ;
        signTure.ResetToBeginning();
    }

    /// <summary>
    /// 设置完成按钮
    /// </summary>
    /// <param name="isShow">是否显示</param>
    /// <param name="des">描述</param>
    /// <param name="callBack">回调</param>
    public void SetFinishBtn(bool isShow = false, EventDelegate callBack = null, string des = "完成任务")
    {
        this.finishBtn.gameObject.SetActive(isShow);
        this.finishBtnLab.text = des;
        this.finishBtn.onClick.Clear();
        if (callBack != null)
        {
            this.finishBtn.onClick.Add(callBack);
        }
    }

    /// <summary>
    /// 设置完成按钮坐标位置
    /// </summary>
    /// <param name="localPos"></param>
    public void SetFinishBtnPos(Vector3 localPos)
    {
        this.finishBtn.transform.localPosition = localPos;
    }

    
    /// <summary>
    /// 设置中断按钮
    /// </summary>
    /// <param name="isShow">是否显示</param>
    /// <param name="des">描述</param>
    /// <param name="callBack">回调</param>
    public void SetBreakBtn(bool isShow = false, EventDelegate callBack = null, string des = "中断任务")
    {
        this.breakBtn.gameObject.SetActive(isShow);
        this.breakBtnLab.text = des;
        this.breakBtn.onClick.Clear();
        if (callBack != null)
        {
            this.breakBtn.onClick.Add(callBack);
        }
    }

    /// <summary>
    /// 设置结束按钮
    /// </summary>
    /// <param name="isShow">是否显示</param>
    /// <param name="des">描述</param>
    /// <param name="callBack">回调</param>
    public void SetEndBtn(bool isShow = false, EventDelegate callBack = null, string des = "结束任务")
    {
        this.endBtn.gameObject.SetActive(isShow);
        this.endBtnLab.text = des;
        this.endBtn.onClick.Clear();
        if (callBack != null)
        {
            this.endBtn.onClick.Add(callBack);
        }
    }

    public void SetShildBtn(bool isShow = false, EventDelegate callBack = null)
    {
        this.shildBtn.gameObject.SetActive(isShow);
        this.shildBtn.onClick.Clear();
        if (callBack != null)
        {
            this.shildBtn.onClick.Add(callBack);
        }
    }

    public void Open()
    {
        Reset();
        this.gameObject.SetActive(true);
    }

    public void Reset()
    {
        SetCountDownTime();
        SetClickCountDownClock();
        SetDragClock();
        SetRotateAngle();
        SetAutoSaveCustomValue();
        SetFinishBtn();
        SetFinishBtnPos(this.originFinishPos);
        SetBreakBtn();
        SetEndBtn();
        SetShildBtn();
    }

    public void Close()
    {
        Reset();
        this.gameObject.SetActive(false);
    }
}
